﻿using LAB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using LAB.Manager;
using System.Data.Entity;
using System.Web.SessionState;
using System.Configuration;
using System.Web.Caching;
namespace VideoTag.Service
{
    /// <summary>
    /// Summary description for TagerOp
    /// </summary>
    public class TagerOp : IHttpHandler, IRequiresSessionState
    {


        string json = "";
        Dictionary<string, Func<HttpContext, string>> dic;
        Cache myCache = HttpRuntime.Cache;

        List<string> sysEditOpList = new List<string>() { "insertVideo", "editVideo", "removeVideo" };


        // subscribe interval check
        Func<int, bool> checkInterval = new Func<int, bool>((myInterval) =>
        {
            var myRst = true;
            var intervalList = ConfigurationManager.AppSettings["subsIntervalList"].Split('-');
            myRst = intervalList.Any(c => c == myInterval.ToString());
            return myRst;
        });

        public void ProcessRequest(HttpContext context)
        {
            initDic();
            context.Response.ContentType = "application/json";
            try
            {
                var op = context.Request["op"];
                bool flag = true;
                if (sysEditOpList.Count(c => c == op) > 0)
                {

                    var editor = SysUtil.getLoginInfo();
                    if (string.IsNullOrEmpty(editor) || !SysUtil.isSysEditor(editor))
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    json = dic[op](context);
                }
                else
                {
                    json = jsonMake(false, "非系统管理员!!");
                }
            }
            catch (Exception e)
            {
                json = jsonMake(false, e.Message);
            }

            context.Response.Write(json);
        }

        void initDic()
        {
            dic = new Dictionary<string, Func<HttpContext, string>>();
            dic.Add("getMethodNameList", (ctx) =>
            {
                return jsonMake(true, string.Join("|", dic.Select(c => c.Key).ToArray()));
            });

            #region UserInfo
            #region getUserInfo
            dic.Add("getUserInfo", (ctx) =>
            {
                var rst = "";
                var userInfo = SysUtil.getLoginInfo();
                rst = jsonMake(true, userInfo);
                return rst;
            });
            #endregion
            #region reg
            dic.Add("reg", (ctx) =>
            {
                var rst = "";
                string userName = ctx.Request["userName"];
                string pwd = ctx.Request["pwd"];
                string vcode = ctx.Request["vcode"];

                if (ctx.Session["vcode"] == null || vcode.ToLower() != ctx.Session["vcode"].ToString().ToLower())
                {
                    return jsonMake(false, "验证码校验失败!");
                }

                int code = UserManager.Insert(userName, pwd);
                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "注册成功,请查看你的邮箱,以激活你的账号!!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "该账号已经被注册!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });

            #endregion

            #region login
            dic.Add("login", (ctx) =>
            {
                var rst = "";
                string userName = ctx.Request["userName"];
                string pwd = ctx.Request["pwd"];
                t_user user = UserManager.Login(userName, pwd);
                if (user == null)
                {
                    return jsonMake(false, "没有对应的用户");
                }
                SysUtil.setLoginInfo(user.userName);
                rst = jsonMake(true, user);
                return rst;
            });
            #endregion

            #region logout
            dic.Add("logout", (ctx) =>
            {
                var rst = "";
                int code = -1;
                string userName = SysUtil.getLoginInfo();
                if (!string.IsNullOrEmpty(userName))
                {

                    SysUtil.setLoginInfo(null);
                    code = 0;
                }
                else
                {
                    code = -1;
                }
                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "登出成功!!");
                dicInfo.Add(-1, "您尚未登录!!");
                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });
            #endregion

            #region changePwd
            dic.Add("changePwd", (ctx) =>
            {
                var rst = "";
                int code = -1;
                string user = SysUtil.getLoginInfo();
                if (string.IsNullOrEmpty(user))
                {
                    code = -4;
                }
                else
                {
                    var userId = UserManager.getUserIdByUserName(user);
                    var exPwd = ctx.Request["exPwd"];
                    var newPwd = ctx.Request["newPwd"];
                    code = UserManager.ChangePassword(userId, exPwd, newPwd);
                }
                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "修改成功!!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "不存在的用户!!");
                dicInfo.Add(-3, "旧密码验证错误!!");
                dicInfo.Add(-4, "你尚未登录!!");
                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });
            #endregion

            #region verify
            dic.Add("verify", (ctx) =>
           {
               var rst = "";
               int code = -1;

               var userId = int.Parse(ctx.Request["userId"]);
               var dt = DateTime.Parse(ctx.Request["dt"]);
               var md5Code = ctx.Request["md5Code"];

               code = UserManager.verify(userId, dt, md5Code);

               Dictionary<int, string> dicInfo = new Dictionary<int, string>();
               dicInfo.Add(0, "修改成功!!");
               dicInfo.Add(-1, "未知错误!!");
               dicInfo.Add(-2, "不存在的用户!!");
               dicInfo.Add(-3, "你已经被成功验证了!!");
               dicInfo.Add(-8, "验证链接超时!!");
               dicInfo.Add(-9, "验证失败!!");
               string info = dicInfo[code];
               rst = jsonMake(code >= 0, info);
               return rst;
           });
            #endregion


            #endregion
            #region Bags
            #region getBags
            dic.Add("getBags", (ctx) =>
                {
                    var rst = "";
                    var keyWord = ctx.Request["keyWord"];
                    int status = 1;
                    int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                    int pageSize = int.Parse(ctx.Request["pageSize"]);
                    var list = BagManager.search(keyWord, status, pageIndex, pageSize);
                    var count = list.Count();
                    rst = jsonMake(new List<object> { list, count });
                    return rst;

                });
            #endregion

            #region getFrozenBags
            dic.Add("getFrozenBags", (ctx) =>
               {
                   var rst = "";
                   var keyWord = ctx.Request["keyWord"];
                   int status = 0;
                   int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                   int pageSize = int.Parse(ctx.Request["pageSize"]);
                   var list = BagManager.search(keyWord, status, pageIndex, pageSize);
                   var count = list.Count();
                   rst = jsonMake(new List<object> { list, count });
                   return rst;

               });
            #endregion

            #region insertBag
            dic.Add("insertBag", (ctx) =>
             {
                 var rst = "";
                 string name = ctx.Request["name"];
                 string editor = SysUtil.getLoginInfo();
                 if (!string.IsNullOrEmpty(name))
                 {

                     var flag = BagManager.insert(name, editor);
                     rst = jsonMake(flag, null);
                 }
                 return rst;
             });
            #endregion

            #region editBag
            dic.Add("editBag", (ctx) =>
            {
                var rst = "";
                int bagId = int.Parse(ctx.Request["bagId"]);
                int status = int.Parse(ctx.Request["status"]);
                string editor = SysUtil.getLoginInfo();

                var flag = BagManager.edit(bagId, status, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion

            #region deleteBag

            dic.Add("deleteBag", (ctx) =>
            {
                var rst = "";
                int bagId = int.Parse(ctx.Request["bagId"]);
                string editor = SysUtil.getLoginInfo();
                if (string.IsNullOrEmpty(editor) || !SysUtil.isSysEditor(editor))
                {
                    return jsonMake(false, "非系统管理员!!");
                }
                var flag = BagManager.delete(bagId, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion



            #endregion
            #region Tags

            #region getTags


            dic.Add("getAllTags", (ctx) =>
               {
                   var rst = "";
                   List<t_tag> list = getTags();
                   rst = jsonMake(true, list);
                   return rst;
               });

            dic.Add("getTagsByBagIds", (ctx) =>
               {
                   var rst = "";
                   int count = 0;
                   List<int> bagIds = string.IsNullOrEmpty(ctx.Request["bagIds"]) ? null : ctx.Request["bagIds"].Split(',').Select(c => int.Parse(c)).ToList<int>();
                   int status = 1;
                   int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                   int pageSize = int.Parse(ctx.Request["pageSize"]);
                   var list = TagManager.search(bagIds, status, pageIndex, pageSize, out count);
                   rst = jsonMake(new List<object> { list, count });
                   return rst;
               });

            // 获取被冻结的tag
            dic.Add("getFrozenTags", (ctx) =>
            {
                var rst = "";
                int status = 0;
                int count = 0;
                int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                int pageSize = int.Parse(ctx.Request["pageSize"]);
                var list = TagManager.search(null, status, pageIndex, pageSize, out count);
                rst = jsonMake(new List<object> { list, count });
                return rst;
            });

            dic.Add("getTagsForAdmin", (ctx) =>
            {
                var rst = "";
                int count = 0;
                List<int> bagIds = string.IsNullOrEmpty(ctx.Request["bagIds"]) ? null : ctx.Request["bagIds"].Split(',').Select(c => int.Parse(c)).ToList<int>();
                int status = int.Parse(ctx.Request["status"]);

                int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                int pageSize = int.Parse(ctx.Request["pageSize"]);
                var list = TagManager.search(bagIds, status, pageIndex, pageSize, out count);
                rst = jsonMake(new List<object> { list, count });
                return rst;
            });


            dic.Add("getHotTags", (ctx) =>
            {
                var rst = "";
                var count = int.Parse(ctx.Request["count"]);
                List<t_tag> list = TagManager.getHotTags(count);
                rst = jsonMake(true, list);
                return rst;
            });

            #endregion

            #region insertTag
            dic.Add("insertTag", (ctx) =>
                {
                    var rst = "";
                    int bagId = int.Parse(ctx.Request["bagId"]);
                    string name = ctx.Request["name"];
                    string editor = SysUtil.getLoginInfo();
                    var flag = TagManager.insert(bagId, name, editor);
                    rst = jsonMake(flag, null);
                    return rst;
                });
            #endregion
            #region editTag
            dic.Add("editTag", (ctx) =>
                {
                    var rst = "";
                    int tagId = int.Parse(ctx.Request["tagId"]);
                    int status = int.Parse(ctx.Request["status"]);
                    string editor = SysUtil.getLoginInfo();
                    if (string.IsNullOrEmpty(editor) || !SysUtil.isSysEditor(editor))
                    {
                        return jsonMake(false, "非系统管理员!!");
                    }
                    var flag = TagManager.edit(tagId, status, editor);
                    rst = jsonMake(flag, null);
                    return rst;
                });
            #endregion

            #region deleteTag
            dic.Add("deleteTag", (ctx) =>
            {
                var rst = "";
                var tagId = int.Parse(ctx.Request["tagId"]);
                string editor = SysUtil.getLoginInfo();
                if (string.IsNullOrEmpty(editor) || !SysUtil.isSysEditor(editor))
                {
                    return jsonMake(false, "非系统管理员!!");
                }
                bool flag = TagManager.delete(tagId, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion

            #endregion
            #region videos
            #region insertVideo
            dic.Add("insertVideo", (ctx) =>
             {
                 var rst = "";
                 string name = ctx.Request["name"];
                 string url = ctx.Request["url"];
                 List<int> tagIds = string.IsNullOrEmpty(ctx.Request["tagIds"]) ? null : ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();
                 string editor = SysUtil.getLoginInfo();
                 var flag = VideoManager.insertWithTagIds(name, url, tagIds, editor);

                 rst = jsonMake(flag, null);
                 return rst;
             });
            #endregion


            #region editVideo
            dic.Add("editVideo", (ctx) =>
             {
                 var rst = "";
                 int videoId = int.Parse(ctx.Request["videoId"]);
                 string name = ctx.Request["name"];
                 List<int> tagIds = string.IsNullOrEmpty(ctx.Request["tagIds"]) ? null : ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();

                 int status = int.Parse(ctx.Request["status"]);
                 string url = ctx.Request["url"];
                 string editor = SysUtil.getLoginInfo();

                 var flag = VideoManager.edit(videoId, name, tagIds, url, status, editor);
                 rst = jsonMake(flag, null);
                 return rst;
             });
            #endregion

            // 这里特殊下,支持传入一串id来删除
            #region deleteVideo
            dic.Add("deleteVideo", (ctx) =>
              {
                  var rst = "";
                  var videoId = int.Parse(ctx.Request["videoId"]);
                  string editor = SysUtil.getLoginInfo();
                  VideoManager.delete(videoId, editor);
                  rst = jsonMake(true, null);
                  return rst;
              });
            #endregion

            #region addVideoTags
            dic.Add("addVideoTags", (ctx) =>
           {
               var rst = "";
               int videoId = int.Parse(ctx.Request["videoId"]);
               var tagIds = string.IsNullOrEmpty(ctx.Request["tagIds"]) ? null : ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();
               string editor = SysUtil.getLoginInfo();
               var flag = VideoManager.addTags(videoId, tagIds, editor);
               rst = jsonMake(flag, null);
               return rst;
           });
            #endregion

            #region removeVideoTags
            dic.Add("removeVideoTags", (ctx) =>
            {
                var rst = "";
                int videoId = int.Parse(ctx.Request["videoId"]);
                var tagIds = ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();
                string editor = SysUtil.getLoginInfo();
                var flag = VideoManager.removeTags(videoId, tagIds, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion

            #region search
            dic.Add("searchForAdmin", (ctx) =>
            {
                var rst = "";
                List<int> tagIds = string.IsNullOrEmpty(ctx.Request["tagIds"]) ? null : ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();
                int status = int.Parse(ctx.Request["status"]);
                int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                int pageSize = int.Parse(ctx.Request["pageSize"]);
                int total = 0;
                List<fullVideo> videos = VideoManager.search(tagIds, status, pageIndex, pageSize, out total);
                rst = jsonMake(new List<object> { videos, total });
                return rst;
            });


            // 普通玩家的查询 只能查询激活状态的视频
            dic.Add("search", (ctx) =>
            {
                var rst = "";
                int total = 0;
                List<int> tagIds = string.IsNullOrEmpty(ctx.Request["tagIds"]) ? null : ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();
                int status = 1;
                int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                int pageSize = int.Parse(ctx.Request["pageSize"]);
                List<fullVideo> videos = VideoManager.search(tagIds, status, pageIndex, pageSize, out total);

                rst = jsonMake(new List<object> { videos, total });
                return rst;
            });


            #endregion

            #region hotVideo
            dic.Add("getHotVideo", (ctx) =>
            {
                var rst = "";
                List<fullVideo> fullVideoList;
                if (myCache["hotVideo"] == null)
                {
                    var count = int.Parse(ConfigurationManager.AppSettings["hotVideoCount"]);
                    var hotVideos = VideoManager.getHotVideo(count);
                    fullVideoList = hotVideos.Select(c => new fullVideo { video = c, tags = TagManager.searchByVideoId(c.id) }).ToList();

                }
                else
                {
                    fullVideoList = (List<fullVideo>)myCache["hotVideo"];
                }
                rst = jsonMake(true, fullVideoList);
                return rst;
            });
            #endregion

            #endregion

            #region Finder
            #region insertFinder
            dic.Add("insertFinder", (ctx) =>
            {
                var rst = "";
                int code;
                if (string.IsNullOrEmpty(ctx.Request["tagIds"]))
                {
                    rst = jsonMake(false, "tagId不可以为空");
                }
                else
                {
                    string name = ctx.Request["name"];
                    List<int> tagIds = ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).ToList();

                    // 处理tagIds
                    var tagsCache = getTags();
                    tagIds = tagIds.Where(c => tagsCache.Where(d => d.status == 1).Any(d => d.id == c)).Distinct().ToList();
                    string editor = SysUtil.getLoginInfo();
                    if (string.IsNullOrEmpty(editor))
                    {
                        code = -4;
                    }
                    else
                    {
                        code = FinderManager.insert(name, tagIds, editor);
                    }
                    Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                    dicInfo.Add(0, "添加搜索器成功!!");
                    dicInfo.Add(-1, "未知错误!!");
                    dicInfo.Add(-2, "同名搜索器存在!!");
                    dicInfo.Add(-3, "搜索器数量最多为5个!!");
                    dicInfo.Add(-4, "你尚未登录!!");

                    string info = dicInfo[code];
                    rst = jsonMake(code >= 0, info);

                }
                return rst;
            });
            #endregion

            #region editFinder
            dic.Add("editFinder", (ctx) =>
            {
                var rst = "";
                int code;
                if (string.IsNullOrEmpty(ctx.Request["tagIds"]))
                {
                    code = -12;
                }
                else
                {

                    int finderId = int.Parse(ctx.Request["finderId"]);
                    string name = ctx.Request["name"];
                    List<int> tagIds = ctx.Request["tagIds"].Split(',').Select(c => int.Parse(c)).Distinct().ToList();

                    // 处理tagIds
                    var tagsCache = getTags(true);
                    tagIds = tagIds.Where(c => tagsCache.Where(d => d.status == 1).Any(d => d.id == c)).ToList();

                    string editor = SysUtil.getLoginInfo();
                    if (string.IsNullOrEmpty(editor))
                    {
                        code = -4;
                    }
                    else
                    {
                        code = FinderManager.edit(finderId, name, tagIds, editor);
                    }
                }
                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "修改搜索器成功!!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "不存在这样的搜索器!!");
                dicInfo.Add(-3, "没有修改权限!!");
                dicInfo.Add(-4, "你尚未登录!!");
                dicInfo.Add(-12, "标签列表不可以为空!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });
            #endregion

            // 这里特殊下,支持传入一串id来删除
            #region deleteFinder
            dic.Add("deleteFinder", (ctx) =>
            {
                var rst = "";
                int code;
                int finderId = int.Parse(ctx.Request["finderId"]);
                string editor = SysUtil.getLoginInfo();
                if (string.IsNullOrEmpty(editor))
                {
                    code = -4;
                }
                else
                {
                    code = FinderManager.delete(finderId, editor);
                }
                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "删除搜索器成功!!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "不存在这样的搜索器!!");
                dicInfo.Add(-3, "没有修改权限!!");
                dicInfo.Add(-4, "你尚未登录!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });
            #endregion



            #region search
            dic.Add("searchFinder", (ctx) =>
            {
                var rst = "";
                string editor = SysUtil.getLoginInfo();
                List<t_finder> list = FinderManager.search(editor);
                rst = jsonMake(true, list);
                return rst;
            });

            dic.Add("searchFinderForAdmin", (ctx) =>
            {
                var rst = "";
                var userLike = ctx.Request["user"];
                var status = int.Parse(ctx.Request["status"]);
                int pageIndex = int.Parse(ctx.Request["pageIndex"]);
                int pageSize = int.Parse(ctx.Request["pageSize"]);
                int total = 0;
                List<v_finder> list = FinderManager.search(userLike, status, pageIndex, pageSize, out total);
                rst = jsonMake(new List<object> { list, total });
                return rst;
            });

            #endregion

            #region rank
            dic.Add("rankFinder", (ctx) =>
             {
                 var rst = "";

                 var ids = ctx.Request["ids"].Split(',').Select(c => int.Parse(c)).ToList();
                 var ranks = ctx.Request["rank"].Split(',').Select(c => int.Parse(c)).ToList();
                 // 检验
                 if (ranks.Count() == ids.Count())
                 {
                     string editor = SysUtil.getLoginInfo();
                     var flag = FinderManager.rank(ids, ranks, editor);
                     rst = jsonMake(flag, null);
                 }
                 return rst;
             });
            #endregion
            #endregion

            #region Subscribe
            #region insertFinder

            dic.Add("insertSubscribe", (ctx) =>
           {
               var rst = "";
               int finderId = int.Parse(ctx.Request["finderId"]);
               int interval = int.Parse(ctx.Request["interval"]);
               if (!checkInterval(interval))
               {
                   return jsonMake(false, "不正确的订阅周期!!");
               }
               string editor = SysUtil.getLoginInfo();
               var flag = SubscribeManager.insert(finderId, interval, editor);
               rst = jsonMake(flag, null);
               return rst;
           });
            #endregion

            #region editSubscribe
            dic.Add("editSubscribe", (ctx) =>
            {
                var rst = "";
                int finderId = int.Parse(ctx.Request["finderId"]);
                int interval = int.Parse(ctx.Request["interval"]);

                if (!checkInterval(interval))
                {
                    return jsonMake(false, "不正确的订阅周期!!");
                }
                string editor = SysUtil.getLoginInfo();
                var flag = SubscribeManager.edit(finderId, interval, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion

            // 这里特殊下,支持传入一串id来删除
            #region deleteSubscribe
            dic.Add("deleteSubscribe", (ctx) =>
            {
                var rst = "";
                var subscribeId = int.Parse(ctx.Request["subscribeId"]);
                string editor = SysUtil.getLoginInfo();
                var flag = SubscribeManager.delete(subscribeId, editor);
                rst = jsonMake(flag, null);
                return rst;
            });
            #endregion
            #endregion

            #region Syno
            #region getTagIdByName
            dic.Add("getTagIdByName", (ctx) =>
                {
                    string rst = "";
                    string synoName = ctx.Request["synoName"].ToLower();
                    int tagId = SynoManager.getTagIdByName(synoName);
                    rst = jsonMake(true, tagId);
                    return rst;
                });

            #endregion

            #region getSynosByTagId
            dic.Add("getSynosByTagId", (ctx) =>
            {
                string rst = "";
                int tagId = int.Parse(ctx.Request["tagId"]);
                List<t_syno> synos = SynoManager.getSynosByTagId(tagId);
                rst = jsonMake(true, synos);
                return rst;
            });

            #endregion

            #region insert
            dic.Add("insert", (ctx) =>
            {
                string rst = "";
                int tagId = int.Parse(ctx.Request["tagId"]);
                string synoName = ctx.Request["synoName"];
                int code = SynoManager.insert(tagId, synoName);

                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "插入同义词成功!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "标签不存在或则标签未审核!!");
                dicInfo.Add(-3, "同义词已经存在!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });

            #endregion

            #region edit
            dic.Add("edit", (ctx) =>
            {
                string rst = "";
                int synoId = int.Parse(ctx.Request["synoId"]);
                int status = int.Parse(ctx.Request["status"]);
                string editor = SysUtil.getLoginInfo();
                int code = SynoManager.edit(synoId, status, editor);

                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "修改同义词成功!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "不存在的同义词!!");
                dicInfo.Add(-3, "你不是系统管理员!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });

            #endregion

            #region delete
            dic.Add("delete", (ctx) =>
            {
                string rst = "";
                int synoId = int.Parse(ctx.Request["synoId"]);
                int code = SynoManager.delete(synoId);

                Dictionary<int, string> dicInfo = new Dictionary<int, string>();
                dicInfo.Add(0, "删除同义词成功!");
                dicInfo.Add(-1, "未知错误!!");
                dicInfo.Add(-2, "不存在的同义词!!");

                string info = dicInfo[code];
                rst = jsonMake(code >= 0, info);
                return rst;
            });

            #endregion
            #endregion
        }

        string jsonMake(bool flag, string msg)
        {
            return string.Format("{{\"flag\":{0},\"msg\":\"{1}\"}}", flag.ToString().ToLower(), msg == null ? "none" : msg.Replace(Environment.NewLine, ""));
        }
        string jsonMake(bool flag, object data)
        {
            return string.Format("{{\"flag\":{0},\"data\":{1}}}", flag.ToString().ToLower(), new JavaScriptSerializer().Serialize(data));
        }

        string jsonMake(List<object> objs)
        {
            return string.Format("{{\"flag\":{0},\"data\":{1}}}", true.ToString().ToLower(), new JavaScriptSerializer().Serialize(objs));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        List<t_tag> getTags(bool flag)
        {
            int count = 0;
            List<t_tag> rst = null;
            if (myCache["tags"] == null || flag)
            {
                var list = TagManager.search(null, -1, 0, int.MaxValue, out count);
                myCache["tags"] = list;
            }
            rst = (List<t_tag>)HttpContext.Current.Cache["tags"];
            return rst;
        }

        List<t_tag> getTags()
        {
            return getTags(false);
        }

        List<t_bag> getBags()
        {
            List<t_bag> rst = null;
            if (myCache["bags"] == null)
            {
                var list = BagManager.search(null, -1, 0, int.MaxValue);
                myCache["bags"] = list;
            }
            rst = (List<t_bag>)HttpContext.Current.Cache["bags"];
            return rst;
        }


    }
}